package com.sc.smartcommunity.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.smartcommunity.domain.Activity;

import com.sc.smartcommunity.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * 分页查询活动
     */
    @GetMapping
    public Map<String, Object> getActivities(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String title) {

        Page<Activity> page = new Page<>(pageNum, pageSize);
        IPage<Activity> activityIPage = activityService.getPage(page, title);

        Map<String, Object> result = new HashMap<>();
        result.put("total", activityIPage.getTotal());
        result.put("list", activityIPage.getRecords());
        result.put("pages", activityIPage.getPages());

        return result;
    }
}
